package com.lanou3g.k12.service.impl;


import com.lanou3g.k12.entity.*;
import com.lanou3g.k12.mapper.CarouselMapper;
import com.lanou3g.k12.service.CarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * carousel-controller 轮播图管理 service层 实现类
 */
@Service
public class CarouselServiceImpl implements CarouselService {
    /**
     * 自动注入 主分支测试
     */
    @Autowired
    public CarouselMapper carouselMapper;

    //查询所有库 所有数据
    public List<Carousel> findAll() {
        //数据库查询所有轮播图信息 返回list 集合
        List<Carousel> all = carouselMapper.findAll();
        return all;
    }

    //通过id查询对应轮播图信息
    public List<Carousel> carouselById(Integer[] id) {
        List<Carousel> carousel = null;
        for (Integer integer : id) {
            carousel = carouselMapper.findById(integer);
        }
        return carousel;
    }

    //通过id 修改轮播图信息
    public Boolean updateById(Integer id, Carousel carousel) {
        Date date = new Date();
        carousel.setUpdateTime(date);
        if (carousel.getSort() == null) {
            carousel.setSort(1);
        }
        Integer sort = carousel.getSort();
        Carousel carousel1 = carouselMapper.findBySort(sort);
        int i = 0;
        if (carousel1 == null) {
            i = carouselMapper.upById(id, carousel);
        } else {
            carouselMapper.updateAferCarousel(sort);
            i = carouselMapper.upById(id, carousel);
        }
        Boolean src = null;
        //if 代码 简化
        src = i == 1;
        return src;
    }

    @Override
    public int delById(Integer id) {
        //通过id删除
        return carouselMapper.delById(id);
    }

    @Override
    public Boolean add(Carousel carousel) {
        Date date = new Date();
        carousel.setCreateTime(date);
        carousel.setUpdateTime(date);
        if (carousel.getSort() == null) {
            carousel.setSort(1);
        }
        Integer sort = carousel.getSort();
        Carousel carousel1 = carouselMapper.findBySort(sort);
        System.out.println("查询sort结果" + carousel1);
        int i = 0;
        if (carousel1 != null) {
            carouselMapper.updateAferCarousel(sort); //更改id大的
            i = carouselMapper.addCarousel(carousel);
        } else {
            i = carouselMapper.addCarousel(carousel);//存入
        }
        Boolean str = null;
        if (i > 0) {
            str = true;
        } else {
            str = false;
        }
        return str;
    }


    /**
     * 首页轮播图
     *
     * @param size 需要首页轮播图张数
     * @return 查询结果
     */
    @Override
    public List<Carousel> showBySort() {
        return carouselMapper.showBySort();
    }





}
